package com.jianyun.jyzs.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.jianyun.jyzs.ThisApp;
import com.jianyun.jyzs.bean.EdMessageBean;
import com.jianyun.jyzs.bean.OAItem;
import com.jianyun.jyzs.constant.SysConstant;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mlli on 2017/11/15.
 * 使用sp保存一些本地数据
 */

public class LocalSp {
    Gson gson;
    private SharedPreferences preferences;
    private final static String LOGIN_CACHE = "local_sp_data";
    private final static String ED_MESSAGE_TAG = "ed_message_tag";
    private final static String ED_MESSAGE_TAG_HASNEW = "ed_message_tag_hasnew";//是否显示新消息
    private final static String UserID=LoginCache.getInstance().getUserInfo().getUserId();
    private final static String ED_MESSAGE_COUNT = "ed_message_count";
    private final static String NEWS_COUNT = "news_message_count";
    private final static String OA_AREA = "oa_area";//OA工作区
    private final static String OA_TOP_AREA = "oa_top_area";//OA工作区

    private LocalSp() {
        init();
    }

    private void init() {
        gson = new Gson();
        preferences = ThisApp.applicationContext.getSharedPreferences(LOGIN_CACHE, Context.MODE_PRIVATE);

    }

    public void clearEdMessageList() {
        preferences.edit().putString(ED_MESSAGE_TAG, null).commit();
    }




    private static class SingleTonHolder {
        private static LocalSp sIns = new LocalSp();
    }

    public static LocalSp getInstance() {
        return SingleTonHolder.sIns;
    }

    public void setEdMessageList(EdMessageBean bean) {
        setEdNewMsgTag(true);
        addEdMessageCount();//新消息条数加一
        String edMessageList = preferences.getString(ED_MESSAGE_TAG, null);
        if (TextUtils.isEmpty(edMessageList)) {
            List<EdMessageBean> list = new ArrayList<>();
            list.add(bean);
            String s = gson.toJson(list.toArray());
            preferences.edit().putString(ED_MESSAGE_TAG, s).commit();
        } else {
            Type type = new TypeToken<List<EdMessageBean>>() {
            }.getType();
            List<EdMessageBean> list = gson.fromJson(edMessageList, type);
            list.add(bean);
            String s = gson.toJson(list.toArray());
            preferences.edit().putString(ED_MESSAGE_TAG, s).commit();
        }


    }

    public List<EdMessageBean> getEdMessageList() {
        List<EdMessageBean> list = new ArrayList<>();
        String s = preferences.getString(ED_MESSAGE_TAG, null);
        if (!TextUtils.isEmpty(s)) {
            Type type = new TypeToken<List<EdMessageBean>>() {
            }.getType();
            list = gson.fromJson(s, type);
        }
        return list;
    }

    public void setEdNewMsgTag(boolean b) {
        preferences.edit().putBoolean(UserID, b).commit();
    }

    public boolean getEdNewMsgTag(String userID) {
        return preferences.getBoolean(userID, false);
    }

    public void addEdMessageCount() {
        int oldcount = preferences.getInt(ED_MESSAGE_COUNT, 0);
        preferences.edit().putInt(ED_MESSAGE_COUNT, oldcount + 1).commit();
    }

    public int getEdMessageCount() {
        return preferences.getInt(ED_MESSAGE_COUNT, 0);
    }

    public void clearEdMessageCount() {
        preferences.edit().putInt(ED_MESSAGE_COUNT, 0).commit();
    }

    //通知公告(新闻公告)
    public void addNewsCount() {
        int oldcount = preferences.getInt(NEWS_COUNT, 0);
        preferences.edit().putInt(NEWS_COUNT, oldcount + 1).commit();
    }

    public int getNewsCount() {
        return preferences.getInt(NEWS_COUNT, 0);
    }

    public void clearNewsCount() {
        preferences.edit().putInt(NEWS_COUNT, 0).commit();
    }

    public List<OAItem> getOaMenu() {
        String string = preferences.getString(OA_AREA,null);
        List<OAItem> list = new ArrayList<>();
        if (string!=null){
            list = gson.fromJson(string,new TypeToken<List<OAItem>>() {}.getType());
            return list;
        }else {
            OAItem messageVO = null;
            for (int i = 0; i < SysConstant.menu_title.length; i++){
                messageVO = new OAItem();
                messageVO.setCount(0);
                messageVO.setShowRed(false);
                messageVO.setContext(SysConstant.menu_title[i]);
                list.add(messageVO);
            }
            String s = gson.toJson(list);
            preferences.edit().putString(OA_AREA,s).commit();
            return list;
        }
    }
    public List<OAItem> getTopMenu() {
        String string = preferences.getString(OA_TOP_AREA,null);
        List<OAItem> list = new ArrayList<>();
        if (string!=null){
            list = gson.fromJson(string,new TypeToken<List<OAItem>>() {}.getType());
            return list;
        }else {
            OAItem messageVO = null;
            for (int i = 0; i < SysConstant.top_title.length; i++){
                messageVO = new OAItem();
                messageVO.setCount(0);
                messageVO.setShowRed(false);
                messageVO.setContext(SysConstant.top_title[i]);
                list.add(messageVO);
            }
            String s = gson.toJson(list);
            preferences.edit().putString(OA_TOP_AREA,s).commit();
            return list;
        }
    }
    public void addAreaCount(int index,int count){
        String string = preferences.getString(OA_AREA,null);
        if (string==null)return;
        List<OAItem> list = gson.fromJson(string,new TypeToken<List<OAItem>>() {}.getType());
        OAItem oaItem = list.get(index);
        oaItem.setCount(oaItem.getCount()+count);
        String s = gson.toJson(list);
        preferences.edit().putString(OA_AREA,s).commit();
    }
    public void clearAreaCount(int index){
        String string = preferences.getString(OA_AREA,null);
        if (string==null)return;
        List<OAItem> list = gson.fromJson(string,new TypeToken<List<OAItem>>() {}.getType());
        OAItem oaItem = list.get(index);
        oaItem.setCount(0);
        String s = gson.toJson(list);
        preferences.edit().putString(OA_AREA,s).commit();
    }
    public void addTopCount(int index, int count){
        String string = preferences.getString(OA_TOP_AREA,null);
        if (string==null)return;
        List<OAItem> list = gson.fromJson(string,new TypeToken<List<OAItem>>() {}.getType());
        OAItem oaItem = list.get(index);
        oaItem.setCount(oaItem.getCount()+count);
        String s = gson.toJson(list);
        preferences.edit().putString(OA_TOP_AREA,s).commit();
    }
    public void clearTopCount(int index){
        String string = preferences.getString(OA_TOP_AREA,null);
        if (string==null)return;
        List<OAItem> list = gson.fromJson(string,new TypeToken<List<OAItem>>() {}.getType());
        OAItem oaItem = list.get(index);
        oaItem.setCount(0);
        String s = gson.toJson(list);
        preferences.edit().putString(OA_TOP_AREA,s).commit();
    }
}
